<?php

/**
 * Created by PhpStorm.
 * User: DD
 * Date: 2016/4/17
 * Time: 22:12
 */
namespace app\common\util;
use app\home\model\Site;
use app\home\model\SiteDomain;

class Util
{
    /**
     * 字符串命名风格转换
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
     * @param string $name 字符串
     * @param integer $type 转换类型
     * @return string
     */
    public static function parseName($name, $type = 0)
    {
        if ($type) {
            return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function ($match) {
                return strtoupper($match[1]);
            }, $name));
        } else {
            return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
        }
    }

    public static function now()
    {
        return date('Y-m-d H:i:s');
    }

    public static function getClassName($obj)
    {
        $className = get_class($obj);
        $pos = strrpos($className, '\\');
        if ($pos) {
            $className = Util::parseName(substr($className, $pos + 1), 1);
        }
        return $className;
    }

    public static function getModelName($obj)
    {
        $className = get_class($obj);
        $pos = strrpos($className, '\\');
        if ($pos) {
            $className = Util::parseName(substr($className, $pos + 1), 0);
        }
        return $className;
    }

    public static function getFieldData($data, $fields)
    {
        $rt = [];
        foreach ($fields as $field) {
            if (isset($data[$field])) {
                $rt[$field] = htmlspecialchars($data[$field]);
            }
        }
        return $rt;
    }

    public static function fetchDeliveryInfo($no)
    {
        $url = 'http://www.kuaidi100.com/query?type=baishiwuliu&postid=' . $no . '&id=1&valicode=&temp=0.3029636044896835';
        $newIp = ccnn_new_ip();
        $headers[] = 'CLIENT-IP:' . $newIp;
        $headers[] = 'X-FORWARDED-FOR:' . $newIp;
        $headers[] = 'Host:www.kuaidi100.com';
        $headers[] = 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36';
        $rt = CUrl::httpGet($url, '', $headers);
        return json_decode($rt, true);
    }

    public static function ta($sex)
    {
        return $sex == 1 ? '他' : '她';
    }

    public static function sex($sex)
    {
        return $sex == 1 ? '男' : '女';
    }

    public static function saveWater()
    {
        $savePath = '/home/sites/images/img.9kk.com/config.php';
        $rows = Site::instance()->select();
        $siteDomain = SiteDomain::instance();
        $results = [];
        foreach ($rows as $row) {
            $site_id = $row['id'];
            if ($site_id < 0) {
                $site_id = 0;
            }
            $picDomainArr = $siteDomain->where(['belong_site_id' => $site_id])->getField('pic_domain', true);
            if (!empty($picDomainArr)) {
                foreach ($picDomainArr as $item) {
                    if (!empty($item)) {
                        $results[$item] = $row;
                    }
                }
            }
        }
        file_put_contents($savePath, '<?php return ' . var_export($results, true) . ';');
    }
}